<?php

// $Id: cookie_user_dbc.class.php 1571 2009-01-13 13:08:33Z ogrebg $

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('cookie_user.class.php');


class Cookie_User_DBC extends Cookie_User implements I_DB_Storable {
	private $db_table_prefix;

	function __construct($db_table_prefix = '') {
		$this->set_db_table_prefix($db_table_prefix);
	}


	public function set_db_table_prefix($db_table_prefix) {
		$this->db_table_prefix = $db_table_prefix;
	}


	public function get_db_table_prefix() {
		return $this->db_table_prefix;
	}


	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		$ret = false;

		$sql = "select cookie, user, ts ".
							"from ".$this->get_db_table_prefix()."cookies_users where id = $id";

		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();
			$this->set_id($id);
			$this->set_cookie($rez_obj->COOKIE);
			$this->set_user($rez_obj->USER);
			$this->set_ts($rez_obj->TS);
			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id($this->get_db_table_prefix()."cookies_users_seq");
		if ($id) {

			$cookie = $this->get_cookie();
			$user = $this->get_user();
			$ts = time();
			$this->set_ts($ts);
			$sql = "insert into ".$this->get_db_table_prefix()."cookies_users"
								."(id, "
								."cookie, "
								."user, "
								."ts "
								.") "
							."values ("
								."$id, "
								."$cookie, "
								."$user,"
								."$ts"
								.")";
			$dbc->execute($sql);

			$ret = $id;
		} else {
			throw new TE_Exception("ID not generated - ".$this->get_db_table_prefix()."cookies_users_seq");
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$cookie = $this->get_cookie();
		$user = $this->get_user();
		$ts = $this->get_ts();

		$sql = "update ".$this->get_db_table_prefix()."cookies_users set "
							."cookie = $cookie, "
							."user = $user, "
							."ts = $ts "
					."where id = ".$this->get_id();

		$dbc->execute($sql);

		return $this->get_id();
	}


	public function load_by_cookie_id(DB_Connection $dbc, $cookie_id) {
		tangra_if_not_int_throw_e($cookie_id);

		$ret = false;

		$sql = "select id from ".$this->get_db_table_prefix()."cookies_users where cookie = $cookie_id";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id, $prefix = '') {
		tangra_if_not_int_throw_e($id);
		$sql = "delete from ".$prefix."cookies_users where id = $id";
		$dbc->execute($sql);
	}
	
	
	public static function delete_by_cookie(DB_Connection $dbc, $cookie_id, $prefix = '') {
		tangra_if_not_int_throw_e($cookie_id);
		$sql = "delete from ".$prefix."cookies_users where cookie = $cookie_id";
		$dbc->execute($sql);
	}
}